package com.vhall.component.controller.lottery;


import com.vhall.component.controller.lottery.facade.LotteryFacade;
import com.vhall.component.entity.lottery.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.plugin.common.annotation.RedisLock;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.service.lottery.LotteryService;
import com.vhall.component.service.lottery.LotteryUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 要做的。发起端 和 观看端
 * 抽奖controller
 *
 * @author song.dong
 */
@Slf4j
@VssTokenAuth
@VhallComponent
public class LotteryController implements LotteryFacade {

    @Autowired
    private LotteryService lotteryService;

    @Autowired
    private LotteryUserService lotteryUserService;

    @Override
    public String time() {
        return new Date().toString();
    }

    /**
     * 获取可以参与抽奖的人数
     */
    @Override
    public LotteryCountRsp count(LotteryCountReqVO req) {
        LotteryCountRsp rsp = new LotteryCountRsp();
        BeanUtils.copyProperties(req, rsp);
        String count = lotteryUserService.count(req);
        rsp.setCount(count);
        return rsp;
    }

    @Override
    public Long total(LotteryCountReqVO req) {
        return lotteryUserService.total(req);
    }

    /**
     * 搜索符合范围条件的抽奖用户名单
     */
    @Override
    public Map<String, Object> search(LotterySearchReqVO req) {
        List<LotterySearchRspVO> lsrvs = lotteryUserService.search(req);
        Map<String, Object> map = new HashMap<>(8);
        map.put("list", lsrvs);
        map.put("lottery_rule", req.getLotteryRule());
        map.put("keyword", req.getKeyword());
        map.put("lottery_type", req.getLotteryType());
        map.put("room_id", req.getRoomId());
        return map;
    }

    /**
     * 导入数据
     */
    @Override
    public LotteryImportRspVO importUser(LotteryImportReqVO lotteryImportReqVO) {
        return lotteryUserService.importUser(lotteryImportReqVO);
    }

    /**
     * 发起抽奖
     */
    @Override
    public LotteryV4AddRspVO add(LotteryV4AddReqVO req) {
        return lotteryService.add(req);
    }


    /**
     * 抽奖结束
     *
     * @param req
     * @return
     */

    @Override
    @RedisLock(redisKey = "#req.lotteryId", retry = false)
    public LotteryRspVO end(V4LotteryEndReqVO req) {
        LotteryRspVO end;
        end = lotteryService.end(req);
        return end;
    }


    @Override
    public LotteryAwardRspVO award(LotteryAwardReqVO reqVo) {
        return lotteryService.award(reqVo);
    }

    @Override
    public CheckAwardInfoRspVO checkAwardInfo(CheckAwardInfoReqVO reqVo) {
        return lotteryService.checkAwardInfo(reqVo);
    }

    /**
     * 获取抽奖中奖人名单
     *
     * @author song.dong
     */
    @Override
    public WatchLotteryUsersGetRspVO usersGet(V4LotteryUsersGetReqVO reqVo) {
        return lotteryService.usersGet(reqVo);
    }

    /**
     * 中奖
     */
    @Override
    @RedisLock(redisKey = "#reqVo.lotteryId", expireTime = 10000)
    public String publish(V4LotteryPublishReqVO reqVo) {
        return lotteryService.publish(reqVo);
    }

    @Override
    public LotteryInfoVO info(V4LotteryPublishReqVO reqVo) {
        return lotteryService.info(reqVo);
    }

    @VssTokenAuth
    @Override
    public LotteryWinningInfo winningInfo(V4LotteryRecordReqVO reqVo) {
        return lotteryService.winningInfo(reqVo);
    }
    @VssTokenAuth
    @Override
    public LotteryRecordVO lotteryWinnerAddress(V4LotteryRecordReqVO reqVo) {
        return null;
    }
}
